package com.future.common.listener.nettysocketio;

import com.corundumstudio.socketio.HandshakeData;
import com.future.model.SysUser;
import com.future.service.AuthenticationService;
import com.future.service.SysUserService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;

import net.minidev.json.JSONObject;
/**
 * 身份验证
 * @author Leon.Zhang
 *
 */
public class AuthorizationListener implements com.corundumstudio.socketio.AuthorizationListener{
	@Inject
	private AuthenticationService authenticationService;
	@Inject
	private SysUserService sysUserService;
	
	/**
	 * 验证身份的合法性
	 */
	@Override
	public boolean isAuthorized(HandshakeData data) {
		String token = data.getSingleUrlParam("Authorization");
		if (StrKit.isBlank(token)) {
			return false;
		}
		JSONObject result = this.authenticationService.parseToken(token);
		String userid = result.getAsString("userid");
		SysUser currentUser = this.sysUserService.findUserById(userid);
		if (currentUser == null) {
			return false;
		}
		return true;
	}

}
